package com.jzqf.harmonydemo.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具类
 * 2020/1/2 10:58
 *
 * @author LiuWeiHao
 * Email 872235631@qq.com
 */
public class DateUtil {

    /**
     * 获取指定日期前或后几天的时间
     *
     * @param day 天数  正数为此日期前的天数
     */
    public static long getCurrentTimeUnixBefore(int day) {
        return System.currentTimeMillis() - (1000 * 60 * 60 * 24) * day;
    }

    /**
     * 获取当天日期
     */
    public static String getCurrentDateStr() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }

    public static String getCurrentDateString() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }

    /**
     * 获取当天日期
     */
    public static String getCurrentDateAndMinuteStr() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }

    /**
     * 获取当前时间
     *
     * @return String yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentTimeStr() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }


    /**
     * string类型日期转时间戳
     *
     * @param dateString 日期字符串
     * @return 时间错
     */
    public static String formatStringToDateUnix(String dateString) {
        if (dateString == null || dateString.length() == 0) {
            return "";
        }
        if (" 00:00:00:000".equals(dateString) || " 23:59:59:999".equals(dateString) || " 23:59:59:000".equals(dateString)) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date;
        try {
            date = dateFormat.parse(dateString);
            return String.valueOf(date.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 日期格式转化
     *
     * @param dateString 字符串日期
     * @return 指定格式后的日期
     */
    public static String formatStringToStr(String dateString) {
        if (dateString == null || dateString.length() == 0) {
            return "";
        }
        if (" 00:00:00:000".equals(dateString) || " 23:59:59:999".equals(dateString) || " 23:59:59:000".equals(dateString)) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date;
        try {
            date = dateFormat.parse(dateString);
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒", Locale.getDefault());
            if (date != null) {
                dateString = format.format(date);
            }
            return dateString;
        } catch (Exception e) {
            e.printStackTrace();
            return dateString;
        }
    }

    public static Date formatStringToDate(String dateString) {
        if (dateString == null || dateString.length() == 0) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date;
        try {
            date = dateFormat.parse(dateString);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Date日期格式化为String类型
     *
     * @param date Date
     * @return String
     */
    public static String formatDate(Date date) {
        String dateString;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ", Locale.getDefault());
        dateString = dateFormat.format(date);
        return dateString;
    }

    /**
     * Date日期格式化为String类型
     *
     * @param date Date
     * @return String
     */
    public static String formatLongDate(Date date) {
        String dateString;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        dateString = dateFormat.format(date);
        return dateString;
    }

    /**
     * 格式化时间戳Unix
     *
     * @param unix Unix时间戳
     * @return String yyyy-MM-dd HH:mm:ss
     */
    public static String formatUnixToString(String unix) {
        if (unix == null || unix.length() == 0) {
            return "";
        }
        String dateString;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        dateString = dateFormat.format(new Date(Long.parseLong(unix)));
        return dateString;
    }

    /**
     * 格式化时间戳Unix
     *
     * @param unix Unix时间戳
     * @return String yyyy-MM-dd
     */
    public static String formatUnixToStringDate(String unix) {
        if (unix == null || unix.length() == 0) {
            return "";
        }
        String dateString;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        dateString = dateFormat.format(new Date(Long.parseLong(unix)));
        return dateString;
    }

    /**
     * 格式化时间戳Unix
     *
     * @param unix Unix时间戳
     * @return String yyyy-MM-dd
     */
    public static String formatUnixToStringDate(long unix) {
        String dateString;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        dateString = dateFormat.format(new Date(unix));
        return dateString;
    }

    /**
     * 格式化时间 时间戳转date
     */
    public static String formatLongDate(long date) {
        return date == 0 ? "" : formatLongDate(new Date(date));
    }

    /**
     * 计算日期差
     *
     * @param startDateString 开始日期
     * @param endDateString   结束日期
     * @return 天数=后日期-前日期
     */
    public static long getDateBetween(String startDateString, String endDateString) {
        long days = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date startDate = dateFormat.parse(startDateString);
            Date endDate = dateFormat.parse(endDateString);
            long diff = endDate.getTime() - startDate.getTime();
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(String nowTime, String startTime, String endTime) {
        String format = "HH:mm";
        if (nowTime.equals(startTime) || nowTime.equals(endTime)) {
            return true;
        }
        try {
            Date nowTimeDate = new SimpleDateFormat(format).parse(nowTime);
            Calendar date = Calendar.getInstance();
            date.setTime(nowTimeDate);

            Date startDateTime = new SimpleDateFormat(format).parse(startTime);
            Calendar begin = Calendar.getInstance();
            begin.setTime(startDateTime);

            Date endDateTime = new SimpleDateFormat(format).parse(endTime);
            Calendar end = Calendar.getInstance();
            end.setTime(endDateTime);
            return date.after(begin) && date.before(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getCurTimeStr() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }

    private static final String TIME_FORMAT = "%02d";

    /**
     * conversion of msToString
     *
     * @param ms ms
     * @return string
     */
    public static String msToString(int ms) {
        String SEMICOLON = ":";
        int ONE_MINS_MINUTES = 60;
        StringBuilder sb = new StringBuilder(16);
        int seconds = ms / 1000;
        int minutes = seconds / ONE_MINS_MINUTES;
        if (minutes > ONE_MINS_MINUTES) {
            sb.append(String.format(Locale.ENGLISH, TIME_FORMAT, minutes / ONE_MINS_MINUTES));
            sb.append(SEMICOLON);
            sb.append(String.format(Locale.ENGLISH, TIME_FORMAT, minutes % ONE_MINS_MINUTES));
            sb.append(SEMICOLON);
        } else {
            sb.append("00:");
            sb.append(String.format(Locale.ENGLISH, TIME_FORMAT, minutes));
            sb.append(SEMICOLON);
        }

        if (seconds > minutes * ONE_MINS_MINUTES) {
            sb.append(String.format(Locale.ENGLISH, TIME_FORMAT, seconds - minutes * ONE_MINS_MINUTES));
        } else {
            sb.append("00");
        }
        return sb.toString();
    }
}
